<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Groups
 * @description : Group list, Group members, Group events
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Groups extends AUTH_Controller {

  public $comitte_members = NULL;
  public $is_comitte_member = FALSE;

  public function __construct() {
    parent::__construct();
    define('NOW_YEAR', date('Y'));
    $comitte_members = $this->config->item('comittee_members');
    if (in_array($this->current_user['user_name'], $comitte_members)) {
      $this->comitte_members = $comitte_members;
      $this->is_comitte_member = TRUE;
    }
  }

  public function index() {
    redirect('groups/group_list/' . NOW_YEAR);
  }

  /**
   * Show groups list
   * 
   */
  public function group_list($selected_year = NOW_YEAR) {

    // Check year
    if (!empty($selected_year) && is_numeric($selected_year)) {

      // Get all groups
      $get_groups = $this->group_model->get_groups($selected_year);
      $groups = $get_groups['result'];

      // Get top groups
      $get_groups = $this->group_model->get_groups($selected_year, 3);
      $top_groups = $get_groups['result'];

      // Getting group years
      $get_group_years = $this->group_model->get_group_years();
      $group_years = $get_group_years['result'];

      // Get group events
      $get_group_events = $this->group_model->get_group_events($selected_year, NULL, 'asc');
      $group_events = $get_group_events['result'];

      // Calculate group scores
      $total_groups_score = 0;
      if (!empty($group_events)) {
        foreach ($group_events as $group_event) {
          $total_groups_score = (int) $total_groups_score + $group_event['group_event_max_score'];
        }
      }

      // Last modified admin
      $last_accessed = array();
      $get_settings = $this->settings_model->get_settings('groups_last_update');
      if ($get_settings['status'] == 1 && !empty($get_settings['result'])) {
        if (!empty($get_settings['result']['settings_config_value'])) {
          $last_accessed = @json_decode($get_settings['result']['settings_config_value']);
        }
      }

      // Preparing view
      $view_data = array();
      $view_data['menu_active'] = 'groups';
      $view_data['selected_year'] = $selected_year;
      $view_data['option_type'] = NULL;
      $view_data['groups'] = $groups;
      $view_data['top_groups'] = $top_groups;
      $view_data['group_events'] = $group_events;
      $view_data['total_groups_score'] = $total_groups_score;
      $view_data['group_years'] = $group_years;
      $view_data['last_accessed'] = $last_accessed;

      // Loading view
      $this->load_view('group_list', $view_data);
      return;
    }

    // Redirect if error occured
    redirect('groups/group_list');
  }

  /**
   * Group members list
   * 
   */
  public function group_members($selected_year = NOW_YEAR) {

    // Checking year
    if (!empty($selected_year) && is_numeric($selected_year)) {

      // Get groups
      $get_groups = $this->group_model->get_groups($selected_year);
      $groups = $get_groups['result'];

      // Inject group members
      $count = 0;
      if (!empty($groups)) {
        foreach ($groups as $group) {
          $get_group_members = $this->group_model->get_group_members($group['group_id']);
          $groups[$count]['group_members'] = $get_group_members['result'];
          $count++;
        }
      }

      // Get group years
      $get_group_years = $this->group_model->get_group_years();
      $group_years = $get_group_years['result'];

      // Preparing view
      $view_data = array();
      $view_data['menu_active'] = 'groups';
      $view_data['selected_year'] = $selected_year;
      $view_data['option_type'] = NULL;
      $view_data['groups'] = $groups;
      $view_data['group_years'] = $group_years;

      // Loading view
      $this->load_view('group_members', $view_data);
      return;
    }

    // Redirect if error occured
    redirect('groups/group_members');
  }

  /**
   * Show group events
   * 
   */
  public function group_events($selected_year = NOW_YEAR) {

    // Checking year
    if (!empty($selected_year) && is_numeric($selected_year)) {

      // Get groups
      $get_groups = $this->group_model->get_groups($selected_year);
      $groups = $get_groups['result'];

      // Get group events
      $get_group_events = $this->group_model->get_group_events($selected_year);
      $group_events = $get_group_events['result'];

      // Injecting group events
      $count = 0;
      if (!empty($group_events)) {
        foreach ($group_events as $events) {
          $get_group_event_results = $this->group_model->get_group_event_results($events['group_event_id']);
          $group_events[$count]['group_event_results'] = $get_group_event_results['result'];
          $count++;
        }
      }

      // Get group years
      $get_group_years = $this->group_model->get_group_years();
      $group_years = $get_group_years['result'];

      // Preparing view
      $view_data = array();
      $view_data['menu_active'] = 'groups';
      $view_data['selected_year'] = $selected_year;
      $view_data['groups'] = $groups;
      $view_data['option_type'] = NULL;
      $view_data['group_events'] = $group_events;
      $view_data['group_years'] = $group_years;

      // Loading view
      $this->load_view('group_events', $view_data);
      return;
    }

    // Redirect if error occured
    redirect('groups/group_events');
  }

}

?>